import router from "./index";
import { getToken, removeToKen,removeUserName } from "@/utils/app";
import store from './index'

//路由守卫
const whiteRouter = ["/login"]; //indexof方法，判断数组中是否存在某个指定的对象，如果不存在，则返回 -1
// 路由守卫
router.beforeEach((to, from, next) => {
  if (getToken()) {
    if(to.path === '/login') {
      removeToKen();
      removeUserName();
      store.commit("app/SET_TOKEN",'')
      store.commit("app/SET_USERNAME",'')
      next()
    }else{
      next()
    }
    console.log("存在");
  } else {
    console.log("不存在");
    if (whiteRouter.indexOf(to.path) !== -1) {
      next();
    } else {
      next("/login");
    }
  }

  // console.log(to);  // 跳转的下一个页面
  // console.log(from); //跳转前的页面
  // console.log(next);
  // next()  //  没参数则执行 to
});
